package com.atguigu.jinx.enums.alipay;


/**
 * 支付宝公共错误码枚举
 */
public enum AlipayErrorCode {

    SUCCESS("10000", "接口调用成功"),
    SERVICE_UNAVAILABLE("20000", "服务不可用"),
    INSUFFICIENT_AUTHORIZATION("20001", "授权权限不足"),
    MISSING_REQUIRED_ARGUMENT("40001", "缺少必选参数"),
    INVALID_ARGUMENT("40002", "非法的参数"),
    BUSINESS_FAILED("40004", "业务处理失败"),
    INSUFFICIENT_PERMISSIONS("40006", "权限不足"),
    UNKNOWN_ERROR("99999", "未知错误");

    private final String code;
    private final String message;

    AlipayErrorCode(String code, String message) {
        this.code = code;
        this.message = message;
    }

    public String getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    /**
     * 根据错误码获取枚举
     */
    public static AlipayErrorCode fromCode(String code) {
        for (AlipayErrorCode errorCode : values()) {
            if (errorCode.code.equals(code)) {
                return errorCode;
            }
        }
        return UNKNOWN_ERROR;
    }

    /**
     * 根据错误码获取枚举
     */
    public static String getMessageFromCode(String code) {
        for (AlipayErrorCode errorCode : values()) {
            if (errorCode.code.equals(code)) {
                return errorCode.getMessage();
            }
        }
        return UNKNOWN_ERROR.getMessage();
    }
}
